package com.banmao.CarlCamp.Day03;

/**
 * https://leetcode.cn/problems/reverse-linked-list/
 */
public class LC206 {


    public ListNode reverseList(ListNode head) {
        if (head == null) {
            return head;
        }
        ListNode dummyHead = new ListNode();
        dummyHead.next = head;

        ListNode pre = head;
        ListNode cur = head.next;
        while (cur != null) {
            ListNode temp = cur.next;
            cur.next = pre;
            pre = cur;
            cur = temp;
        }
        if (dummyHead.next != null) {
            dummyHead.next.next = null;
        }
        return pre;
    }

    static class ListNode {
        int val;
        ListNode next;
        ListNode() {}
        ListNode(int val) { this.val = val; }
        ListNode(int val, ListNode next) { this.val = val; this.next = next; }
    }
}
